package ihm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import client.CommunicationClient;

public class FenetreAffichageMessage extends JFrame{
	
	private static final long serialVersionUID = 1L;
	CommunicationClient comCl;
	String expediteur;
	String message;
	FenetreAffichageMessage fen;
	ThreadActualiseAmis th;
	public FenetreAffichageMessage(String _expediteur,CommunicationClient cl,String _message,
			ThreadActualiseAmis _th){
		comCl=cl;
		th=_th;
		expediteur=_expediteur;
		message=_message;
		fen=this;
		this.setVisible(true);
		this.setTitle("Message de "+expediteur);
		setBounds(150, 150,350,250);
		this.setLayout(new BorderLayout());
		this.setResizable(false);
		
		JPanel p=new JPanel();
		p.setLayout(new BorderLayout());
		JPanel pannMess=new JPanel();
		JTextArea mess=new JTextArea(12,22);
		mess.setText(message);
		mess.setEditable(false);
		pannMess.add(mess);
		pannMess.setBackground(Color.pink);
		
		JPanel pannB=new JPanel();
		JButton boutonFermer=new JButton("Fermer");
		boutonFermer.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					comCl.supprimerMessage(message);
					for(int i=0;i<comCl.getMessages().size();i++){
						if(comCl.getMessages().get(i).getMessage().compareTo(message)==0)
							comCl.getMessages().remove(i);
					}
					th.reafficheListe();
					fen.dispose();
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		pannB.add(boutonFermer);
		p.setLayout(new BorderLayout());
		p.add(pannMess,BorderLayout.NORTH);
		p.add(pannB,BorderLayout.SOUTH);
		add(p,BorderLayout.CENTER);
		
		
		
	}

}
